<?php
	require '../include/library.inc.php';
	require '../include/admin_login.inc.php';
	
	$product_id = intval($_GET['product_id']);
	
	if($product_id) {
	  
	  	$sql = "SELECT cover FROM 
				   products
				WHERE
				   product_id = '$product_id'";
				   
		$result = query($sql);	
		$row = mysql_fetch_object($result);	   
		
		if(file_exists('../product_images/big/'.$row->cover)){
				unlink('../product_images/big/'.$row->cover);
			}
			
		if(file_exists('../product_images/big/'.$row->cover)){
				unlink('../product_images/big/'.$row->cover);
			}
			
		if(file_exists('../product_images/medium/'.$row->cover)){
				unlink('../product_images/medium/'.$row->cover);
			}
		if(file_exists('../product_images/cover/'.$row->cover)){
				unlink('../product_images/cover/'.$row->cover);
			}
			
		
		
		$sql = "DELETE FROM 
				   products
				WHERE
				   product_id = '$product_id'
				 ";
		query($sql);
		
		$sql = "DELETE FROM 
				   product_description
				WHERE
				   product_id = '$product_id'
				 ";
		query($sql);
		
		$sql = "DELETE FROM 
				   sef_url
				WHERE
				   query = '$product_id'
				   AND
				   tamplate = 'product'
				 ";
		query($sql);
		
		
		
		$sql = "DELETE FROM 
				   product_to_category
				WHERE
				   product_id = '$product_id'
				 ";
		query($sql);
		
		
		$sql = "SELECT * FROM 
				  product_gallery
				WHERE
				   product_id = '$product_id'";
				   
		$result = query($sql);	
		while($row_g = mysql_fetch_object($result))
		{
			if(file_exists('../product_images/gallery/'.$row_g->file)){
				unlink('../product_images/gallery/'.$row_g->file);
			}
			
			if(file_exists('../product_images/gallery/th_'.$row_g->file)){
				unlink('../product_images/gallery/th_'.$row_g->file);
			}
			$sql = "DELETE FROM 
				  product_gallery_description
				WHERE
				   image_id = '".$row_g->image_id."'
				 ";
			query($sql);
		}
		
		
		$sql = "DELETE FROM 
				    product_gallery
				WHERE
				   product_id = '$product_id'";
		query($sql);
					
		$_SESSION['message'] = 'Изтриването е успешно!';
	
	}
  
	redirect($_SERVER['HTTP_REFERER']);
 
?>